package gui

import action._
import java.awt.Color
import java.awt.event.ActionEvent
import java.awt.event.KeyEvent
import javax.imageio.ImageIO
import javax.swing.ImageIcon
import javax.swing.KeyStroke
import lang.LOC
import scala.collection.immutable.HashMap
import scala.swing.ButtonGroup
import scala.swing.Menu
import scala.swing.MenuBar
import scala.swing.MenuItem
import scala.swing.RadioMenuItem
import scala.swing.Separator

/**
 * Die Klasse CMenubar erweitert die vorhandene MenuBar-Klasse
 * und passt diese auf die benoetigten Funktionalitaeten
 * des Programms an.
 * 
 * Hier werden die verschiedenen Menueeintraege aus den
 * Language-Dateien geladen und mit den entsprechenden
 * Shortcuts und/oder Icons hinterlegt.
 */

class CMenuBar(tt: Layout) extends MenuBar {
  
  var menulinks:HashMap[(Int, Int), MenuItem] = new HashMap()
    
  //Menugroesse abfragen
  val menusize = augmentString(LOC.get("menusize")).toInt
  
  for (i <- 1 to menusize) {
    contents += new Menu(LOC.get("menu" + i)) {
      peer.setMnemonic(LOC.get("menu" + i).charAt(0))

      var menuitemsize = augmentString(LOC.get("menu" + i + "size")).toInt
      for (j <- 1 to menuitemsize) {
        val tmp = LOC.get("menu" + i + "item" + j)
        if (tmp == "---") {
          contents += new Separator() { peer.setForeground(Color.LIGHT_GRAY) }
        }
        else {
          i match {
            case 1 =>
              var link = new MenuItem(MenuFile(tmp, tt, j)) {
                j match {
                  case 1 =>
                    peer.setAccelerator(KeyStroke.
                                        getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK))
                    peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                              getResource("/gui/img/menubar/new_16.png"))))
                  
                  case 2 =>
                    peer.setAccelerator(KeyStroke.
                                        getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK))
                    peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                              getResource("/gui/img/menubar/open_16.png"))))
                  
                  case 4 =>
                    peer.setAccelerator(KeyStroke.
                                        getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK))
                    peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                              getResource("/gui/img/menubar/save_16.png"))))
                  
                  case 8 =>
                    peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                              getResource("/gui/img/menubar/openext_16.png"))))
                  
                  case 11 =>
                    peer.setAccelerator(KeyStroke.
                                        getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK))
                    peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                              getResource("/gui/img/menubar/printer_16.png"))))
                  
                  case _ =>
                    null
                }
              }
              menulinks += (((i, j),link))
              contents += link
            
            case 2 =>
              var link = new MenuItem(MenuEdit(tmp, tt, j)) {
                j match {
                  case 1 =>
                    peer.setAccelerator(KeyStroke.
                                        getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK))
                    peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                                  getResource("/gui/img/menubar/cut_16.png"))))
          
                  case 2 =>
                    peer.setAccelerator(KeyStroke.
                                        getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK))
                    peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                                 getResource("/gui/img/menubar/copy_16.png"))))
         
                  case 3 =>
                    peer.setAccelerator(KeyStroke.
                                        getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK))
                    peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                                getResource("/gui/img/menubar/paste_16.png"))))
        
                  case 4 =>
                    peer.setAccelerator(KeyStroke.
                                        getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK))
                    
                  case 6 => peer.setAccelerator(KeyStroke.
                                            getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK))
                
                  case _ =>
                    null
                }
              }
              
              menulinks += (((i, j),link))
              contents += link
              
            case 3 =>
              if(tmp == "submenu") {
                var tmp2 = LOC.get("menu" + i + "menu" + j)
                contents += new Menu(tmp2) {
                  var menuitemsize2 =
                                 augmentString(LOC.get("menu" + i + "menu" + j + "size")).toInt
                  var buttongroup = new ButtonGroup()
                  
                  for (k <- 1 to menuitemsize2) {
                    var tmp3 = LOC.get("menu" + i + "menu" + j + "item" + k)
                    buttongroup.buttons += new RadioMenuItem(tmp3) {
                      action = MenuVerify(tmp3, tt, (j + "0" + k).toInt)
                      selected = k == 1
                    }
                  }
                  
                  contents ++= buttongroup.buttons
                }
              }
              else {
                var link = new MenuItem(MenuVerify(tmp, tt, j)) {
                  j match {
                    case 1 =>
                      peer.setAccelerator(KeyStroke.getKeyStroke("F5"))
                      peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                         getResource("/gui/img/menubar/play_ausgabe_16.png"))))
 
                    case 2 =>
                      peer.setAccelerator(KeyStroke.getKeyStroke("F6"))
                      peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                           getResource("/gui/img/menubar/play_trace_16.png"))))
                    
                    case 3 =>
                      peer.setAccelerator(KeyStroke.getKeyStroke("F7"))
                      peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                       getResource("/gui/img/menubar/play_propagate_16.png"))))
                    
                    case 4 =>
                      peer.setAccelerator(KeyStroke.getKeyStroke("F8"))
                      peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                           getResource("/gui/img/menubar/play_prove_16.png"))))
                    
                    case _ =>
                      null
                  }
                }
                menulinks += (((i, j),link))
                contents += link
              }
              
            case 4 =>
              if(tmp == "submenu") {
                var tmp2 = LOC.get("menu" + i + "menu" + j)
                contents += new Menu(tmp2) {
                  var menuitemsize2 =
                                 augmentString(LOC.get("menu" + i + "menu" + j + "size")).toInt
                  var buttongroup2 = new ButtonGroup()
                  for (k <- 1 to menuitemsize2) {
                    var tmp3 = LOC.get("menu" + i + "menu" + j + "item" + k)
                    buttongroup2.buttons += new RadioMenuItem(tmp3) {
                      action = MenuQuest(tmp3, tt, (j + "0" + k).toInt)
                      if(k == 1 && Layout.currentlang == "de")
                        selected = true
                      if(k == 2 && Layout.currentlang == "en")
                        selected = true
                    }
                  }
                  contents ++= buttongroup2.buttons
                }
              }
              else if(tmp == "submenu2") {
                var tmp2 = LOC.get("menu" + i + "menu" + j)
                contents += new Menu(tmp2) {
                  var menuitemsize2 =
                                 augmentString(LOC.get("menu" + i + "menu" + j + "size")).toInt
                  for (k <- 1 to menuitemsize2) {
                    var tmp3 = LOC.get("menu" + i + "menu" + j + "item" + k)
                    contents += new MenuItem(MenuQuest(tmp3, tt, (j + "0" + k).toInt))
                  }
                }
              }
              else {
                var link = new MenuItem(MenuQuest(tmp, tt, j)) {
                  j match {
                    case 1 => peer.setIcon(new ImageIcon(ImageIO.read(this.getClass().
                                                 getResource("/gui/img/menubar/help_16.png"))))
                    case _ => null
                  }
                }
                menulinks += (((i, j),link))
                contents += link
              }
              
            case _ =>
              null
          }
        }
        
      }
    }
  }

  //Funktion fuer das Aktiv/Inaktiv setzen von Menueeintraegen
  def enableItem(set: Boolean, menu: Int, item: Int) = {
    var it = menulinks.get((menu, item)) match {
      case Some(x:MenuItem) => x.enabled = set;
      case _ =>
    }
  }
  
}


 